package service

import (
	"errors"
	"fmt"
	"lhy.org/custom_system/dao"
	"lhy.org/custom_system/model"
)

func ShowMenu() {
	fmt.Println("-----------------客户信息管理软件-----------------")
	dao.ShowMenu()
}

func InputMenu() (key int, err error) {
	fmt.Print("请选择(1-5):")
	_, err = fmt.Scanln(&key)
	fmt.Println()
	return
}

func AddCustom() error {
	var name, email, address string
	var age int
	fmt.Print("请输入姓名:")
	var _, err = fmt.Scanln(&name)
	if err != nil {
		fmt.Println("姓名输入错误")
	}
	fmt.Print("请输入年龄:")
	_, err = fmt.Scanln(&age)
	if err != nil {
		return errors.New("年龄输入错误")
	}
	fmt.Print("请输入邮箱地址:")
	_, err = fmt.Scanln(&email)
	if err != nil {
		return errors.New("邮箱地址输入错误")
	}
	fmt.Print("请输入居住地址:")
	if err != nil {
		return errors.New("居住地址输入错误")
	}
	_, err = fmt.Scanln(&address)
	custom := model.Custom{
		Name:    name,
		Age:     age,
		Email:   email,
		Address: address,
	}
	dao.AddCustom(&custom)
	return nil
}

func UpdateCustom() error {
	var name, email, address string
	var age int
	fmt.Print("请输入姓名:")
	var _, err = fmt.Scanln(&name)
	if err != nil {
		fmt.Println("姓名输入错误")
	}
	custom := dao.GetCustom(name)
	if custom == nil {
		return errors.New("要修改的用户不存在")
	}
	fmt.Print("请输入年龄:")
	_, err = fmt.Scanln(&age)
	if err != nil {
		return errors.New("年龄输入错误")
	}
	fmt.Print("请输入邮箱地址:")
	_, err = fmt.Scanln(&email)
	if err != nil {
		return errors.New("邮箱地址输入错误")
	}
	fmt.Print("请输入居住地址:")
	if err != nil {
		return errors.New("居住地址输入错误")
	}
	_, err = fmt.Scanln(&address)

	custom.Age = age
	custom.Name = name
	custom.Address = address
	custom.Email = email
	return nil
}

func DeleteCustom() error {
	var name string
	fmt.Print("请输入姓名:")
	var _, err = fmt.Scanln(&name)
	if err != nil {
		fmt.Println("姓名输入错误")
	}
	custom := dao.GetCustom(name)
	if custom == nil {
		return errors.New("要删除的用户不存在")
	}
	dao.DeleteCustom(custom)
	return nil
}

func ShowAllCustom() {
	dao.ShowAllCustom()
}
